package com.xingzhi.shortvideosharingplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingzhi.shortvideosharingplatform.mapper.UserBehaviorLogsZMapper;
import com.xingzhi.shortvideosharingplatform.model.UserBehaviorLogs;
import com.xingzhi.shortvideosharingplatform.service.UserBehaviorLogsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class UserBehaviorLogsZServiceImpl extends ServiceImpl<UserBehaviorLogsZMapper, UserBehaviorLogs> implements UserBehaviorLogsService {

    @Override
    public boolean logLikeBehavior(Long userId, Long videoId) {
        // 点赞的behaviorType固定为2，对应user_behavior_types表中的记录
        return logBehavior(userId, videoId, 2L, "点赞");
    }

    @Override
    public boolean logBehavior(Long userId, Long videoId, Long behaviorType, String typeName) {
        try {
            UserBehaviorLogs log = new UserBehaviorLogs();
            log.setUserId(userId);
            log.setVideoId(videoId);
            log.setBehaviorType(behaviorType);
            log.setType(typeName);
            log.setCreatedTime(LocalDateTime.now());
            
            return save(log);
        } catch (Exception e) {
            log.error("记录用户行为失败: userId={}, videoId={}, behaviorType={}", userId, videoId, behaviorType, e);
            return false;
        }
    }
    
    @Override
    public boolean removeUserLikeBehavior(Long userId, Long videoId) {
        try {
            LambdaQueryWrapper<UserBehaviorLogs> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserBehaviorLogs::getUserId, userId)
                      .eq(UserBehaviorLogs::getVideoId, videoId)
                      .eq(UserBehaviorLogs::getBehaviorType, 2L); // 2是点赞行为类型
            
            return remove(queryWrapper);
        } catch (Exception e) {
            log.error("删除用户点赞行为记录失败: userId={}, videoId={}", userId, videoId, e);
            return false;
        }
    }
    
    @Override
    public boolean logFavoriteBehavior(Long userId, Long videoId) {
        // 收藏的behaviorType固定为3，对应user_behavior_types表中的记录
        return logBehavior(userId, videoId, 3L, "收藏");
    }
    
    @Override
    public boolean removeUserFavoriteBehavior(Long userId, Long videoId) {
        try {
            LambdaQueryWrapper<UserBehaviorLogs> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserBehaviorLogs::getUserId, userId)
                      .eq(UserBehaviorLogs::getVideoId, videoId)
                      .eq(UserBehaviorLogs::getBehaviorType, 3L); // 3是收藏行为类型
            
            return remove(queryWrapper);
        } catch (Exception e) {
            log.error("删除用户收藏行为记录失败: userId={}, videoId={}", userId, videoId, e);
            return false;
        }
    }
    
    @Override
    public boolean logFollowBehavior(Long userId, Long followingId) {
        try {
            UserBehaviorLogs log = new UserBehaviorLogs();
            log.setUserId(userId);
            log.setVideoId(followingId); // 在关注行为中，videoId字段存储被关注者ID
            log.setBehaviorType(8L); // 8是关注行为类型
            log.setType("关注");
            log.setCreatedTime(LocalDateTime.now());
            
            return save(log);
        } catch (Exception e) {
            log.error("记录用户关注行为失败: userId={}, followingId={}", userId, followingId, e);
            return false;
        }
    }
    
    @Override
    public boolean removeUserFollowBehavior(Long userId, Long followingId) {
        try {
            LambdaQueryWrapper<UserBehaviorLogs> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserBehaviorLogs::getUserId, userId)
                      .eq(UserBehaviorLogs::getVideoId, followingId) // videoId字段存储被关注者ID
                      .eq(UserBehaviorLogs::getBehaviorType, 8L); // 8是关注行为类型
            
            return remove(queryWrapper);
        } catch (Exception e) {
            log.error("删除用户关注行为记录失败: userId={}, followingId={}", userId, followingId, e);
            return false;
        }
    }

    @Override
    public void recordCommentBehavior(Long userId, Long videoId, Long commentId) {
        try {
            UserBehaviorLogs log = new UserBehaviorLogs();
            log.setUserId(userId);
            log.setVideoId(videoId);
            log.setBehaviorType(6L); // 6是评论行为类型
            log.setType("评论");
            log.setCreatedTime(LocalDateTime.now());
            
            save(log);
        } catch (Exception e) {
            log.error("记录评论行为失败: userId={}, videoId={}, commentId={}", userId, videoId, commentId, e);
        }
    }
    
    @Override
    public void recordLikeCommentBehavior(Long userId, Long videoId, Long commentId) {
        try {
            UserBehaviorLogs log = new UserBehaviorLogs();
            log.setUserId(userId);
            log.setVideoId(videoId);
            log.setBehaviorType(9L); // 9是点赞评论行为类型
            log.setType("点赞评论:"+commentId); // 在type字段中包含评论ID
            log.setCreatedTime(LocalDateTime.now());
            
            save(log);

        } catch (Exception e) {
            log.error("记录点赞评论行为失败: userId={}, videoId={}, commentId={}", userId, videoId, commentId, e);
        }
    }
} 